/****************************************************
	文件：GameStart.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/1 17:42:16
	功能：入口
*****************************************************/

using UnityEngine;
using HTClient.Net;
using HTServer.Proto;

namespace HTClient
{
    public class GameStart : MonoBehaviour
    {
        private string mLocalToken;
        private void Start()
        {
            NetMgr.Instance.Connect("127.0.0.1", 8888);
        }
        private void Update()
        {
            NetMgr.Instance.Update();

            //注册
            if (Input.GetKeyDown(KeyCode.R))
            {
                ProtoMgr.RegisterRequest(RegisterType.Phong, "18171223217", "huskyTgame123", "123456", (res, token) =>
                    {
                        switch (res)
                        {
                            case RegisterResult.Success:
                                Debug.Log("注册成功！\ntoken：" + token);
                                mLocalToken = token;
                                break;
                            case RegisterResult.Failure:
                                Debug.LogError("注册失败");
                                break;
                            case RegisterResult.AlreadyExist:
                                Debug.LogError("用户名已存在");
                                break;
                            case RegisterResult.WrongCode:
                                Debug.LogError("验证码错误");
                                break;
                            case RegisterResult.Forbiddent:
                                Debug.LogError("禁止注册");
                                break;
                        }
                    });
            }
            //登录
            if (Input.GetKeyDown(KeyCode.T))
            {
                ProtoMgr.LoginRequest(LoginType.Phong, "18171223217", "huskyTgame123", mLocalToken, (res, token) =>
                {
                    switch (res)
                    {
                        case LoginResult.Success:
                            Debug.Log("登陆成功！\nOldToken：" + mLocalToken + "\nNewToken：" + token);
                            mLocalToken = token;
                            break;
                        case LoginResult.Failure:
                            Debug.LogError("登陆失败");
                            break;
                        case LoginResult.UserNoExist:
                            Debug.LogError("用户名不存在");
                            break;
                        case LoginResult.WrongPwd:
                            Debug.LogError("密码错误");
                            break;
                    }
                });
            }
            //自动登录
            if (Input.GetKeyDown(KeyCode.Y))
            {
                ProtoMgr.LoginRequest(LoginType.Token, "18171223217", mLocalToken, mLocalToken, (res, token) =>
                {
                    switch (res)
                    {
                        case LoginResult.Success:
                            Debug.Log("自动登陆成功！\nOldToken：" + mLocalToken + "\nNewToken：" + token);
                            mLocalToken = token;
                            break;
                        case LoginResult.Failure:
                            Debug.LogError("登陆失败");
                            break;
                        case LoginResult.UserNoExist:
                            Debug.LogError("用户名不存在");
                            break;
                        case LoginResult.TimeoutToken:
                            Debug.LogError("自动登录超时");
                            break;
                    }
                });
            }

            #region Test
            //分包测试：
            if (Input.GetKeyDown(KeyCode.O))
            {
                ProtoMgr.TestSubcontractRequest();
            }
            //粘包测试：
            if (Input.GetKeyDown(KeyCode.P))
            {
                ProtoMgr.TestStickRequest();
            }
            #endregion
        }
        private void OnApplicationQuit()
        {
            NetMgr.Instance.Close();
        }
    }
}
